﻿<%@ WebHandler Language="C#" Class="xinxi_wuliao" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_wuliao : IHttpHandler {

    public void ProcessRequest(HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }

            string requestType = context.Request.Form["wuLiaoType"];
            string requestWuLiaoLeiXing = context.Request.Form["wuLiaoLeiXing"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                string createCondition = string.Empty;
                switch (requestType)
                {
                    case "1":
                        if (!string.IsNullOrEmpty(requestWuLiaoLeiXing))
                        {
                            createCondition += m_DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, requestWuLiaoLeiXing, "", SearchConditionType.等于);
                        }
                        Dictionary<string, string> orderBy = new Dictionary<string, string>();
                        orderBy.Add("wuliaoleixing", "desc");
                        orderBy.Add("wuliaomingcheng", "asc");
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_wuliao", createCondition, orderBy);
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string wuLiaoMingCheng = context.Request.Form["wuliaomingcheng"];
                        string wuLiaoZhuangTai = context.Request.Form["wuliaozhuangtai"];
                        string wuLiaoLeiXing = context.Request.Form["wuliaoleixing"];
                        string sheZhiFaHuoZhongLiang = context.Request.Form["shezhifahuozhongliang"];
                        string sheZhiQuXiang = context.Request.Form["shezhiquxiang"];
                        string wuLiaoID = context.Request.Form["wuLiaoID"];
                        if (string.IsNullOrEmpty(wuLiaoID))
                        {
                            //新增
                            createCondition = m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, wuLiaoLeiXing, "", SearchConditionType.等于);
                            DataTable wuLiaoDt = m_DB.GetDataTableByCondition("tbl_wuliao", createCondition);
                            if (wuLiaoDt.Rows.Count > 0)
                            {
                                context.Response.Write("已经存在同名物料，请检查重试...");
                                return;
                            }
                            TableRow newRow = new TableRow("tbl_wuliao", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol();
                            col1.ColName = "wuliaomingcheng";
                            col1.ValueMember = wuLiaoMingCheng;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col3 = new TableCol();
                            col3.ColName = "wuliaozhuangtai";
                            col3.ValueMember = wuLiaoZhuangTai;
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "wuliaoleixing";
                            col4.ValueMember = wuLiaoLeiXing;
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "shezhifahuozhongliang";
                            col5.ValueMember = sheZhiFaHuoZhongLiang;
                            col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "shezhiquxiang";
                            col6.ValueMember = sheZhiQuXiang;
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "shangchuanzhuangtai";
                            col7.ValueMember = "1";
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol();
                            col8.ColName = "zhandian";
                            col8.ValueMember = "云端";
                            col8.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col8);
                            TableCol col9 = new TableCol();
                            col9.ColName = "kehubianhao";
                            col9.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col9.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col9);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }

                        }
                        else
                        {
                            //修改
                            createCondition = m_DB.CreateAndCondition("wuliaomingcheng", DataValueCommonType.字符串, wuLiaoMingCheng, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("id", DataValueCommonType.字符串, wuLiaoID, "", SearchConditionType.不等于);
                            createCondition += m_DB.CreateAndCondition("wuliaoleixing", DataValueCommonType.字符串, wuLiaoLeiXing, "", SearchConditionType.等于);
                            DataTable wuLiaoDt = m_DB.GetDataTableByCondition("tbl_wuliao", createCondition);
                            if (wuLiaoDt.Rows.Count > 0)
                            {
                                context.Response.Write("已经存在同名物料，请检查重试...");
                                return;
                            }
                            TableRow editRow = new TableRow("tbl_wuliao", wuLiaoID, DBCtl.修改);
                            TableCol col1 = new TableCol();
                            col1.ColName = "wuliaomingcheng";
                            col1.ValueMember = wuLiaoMingCheng;
                            col1.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col1);
                            TableCol col3 = new TableCol();
                            col3.ColName = "wuliaozhuangtai";
                            col3.ValueMember = wuLiaoZhuangTai;
                            col3.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "wuliaoleixing";
                            col4.ValueMember = wuLiaoLeiXing;
                            col4.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "shezhifahuozhongliang";
                            col5.ValueMember = sheZhiFaHuoZhongLiang;
                            col5.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "shezhiquxiang";
                            col6.ValueMember = sheZhiQuXiang;
                            col6.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "shangchuanzhuangtai";
                            col7.ValueMember = "1";
                            col7.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol();
                            col8.ColName = "zhandian";
                            col8.ValueMember = "云端";
                            col8.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col8);
                            TableCol col9 = new TableCol();
                            col9.ColName = "kehubianhao";
                            col9.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col9.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col9);
                            if (m_DB.SaveTableRows(editRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }

                        }
                        break;
                    case "3":
                        string delWuLiaoID = string.Format("{0}", context.Request.Form["wuLiaoID"]);
                        if (string.IsNullOrEmpty(delWuLiaoID))
                        {
                            context.Response.Write("没有找到物料信息，请检查重试...");
                            return;
                        }
                        string xingHaoCondition = m_DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, delWuLiaoID, "", SearchConditionType.等于);
                        DataTable xingHaoDt = m_DB.GetDataTableByCondition("tbl_xinghao", xingHaoCondition);
                        if (xingHaoDt.Rows.Count > 0)
                        {
                            context.Response.Write("请先删除该物料下的型号信息。");
                            return;
                        }
                        TableRow delRow = new TableRow();
                        delRow.TableName = "tbl_wuliao";
                        delRow.ID = delWuLiaoID;
                        if (m_DB.DeleteRowByID(delRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("Failed");
                        }
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("物料信息异常。{0}", err.Message));
                  context.Response.Write("请重试...");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}